Structures and pointers Example 3: Using
two structures
int main(){
cout << who->addr.number
<<
who->addr.street;
};
float most=factory[0].salary;
struct person *aux, *who;
for (aux=who=factory;
aux<factory+90; aux++)
if (aux->salary > most) {
who = aux;
most = aux->salary;
}
Factory
database:
struct
address {
char street[30];
int number;
};
struct
person {
char name[30];
struct address addr;
float salary;
};
struct
person factory[90];
In each step of
the loop, the current value of most is compared with the salary
field of the current record. The operation
aux++;
moves aux
to the next record because the increment is scaled by the size of the
structure person. Comparing the efficiency of two programs one can notice
that the efficiency of the second program is higher. This technique can not
be used in Pascal because a pointer cannot be mode to point to a particular
element of an array.